<template>
	<view>
		<view class="px-2">
			<uni-section title="转发短信的目标邮箱" subTitle="此处不填写默认会将短信转发至注册时填写的用于找回密码的邮箱；暂不支持多个邮箱转发。" type="line">
				<view class="mb-2">
					<input type="text" v-model="email" placeholder="请输入正确的邮箱号" class="border p-2" />
				</view>

				<view class="fixed-bottom px-3 py-5" style="bottom: 100rpx">
					<button type="warn" class="mt-5" @click="clear">清除邮箱</button>
					<button type="primary" :disabled="disabled" class="mt-5" @click="save">保存</button>
				</view>
			</uni-section>
		</view>
	</view>
</template>

<script setup>
	import {
		ref,
		onMounted,
		computed
	} from 'vue';
	import {
		useStore
	} from 'vuex';
	import $U from '@/common/util.js';

	const store = useStore();

	const {
		targetEmail,
		user
	} = store.state;
	const _email = targetEmail ? targetEmail : user.email;
	const email = ref(_email);

	const disabled = computed(() => {
		if ((email.value === '') || (email.value.length <= 5)) {
			return true
		}
		return false;
	});

	const check = () => {
		return $U.checkEmail(email.value, {
			required: false
		});
	}

	const save = () => {
		if (check()) {
			uni.setStorageSync('targetEmail', email.value);
			store.commit('setTargetEmail', email.value);
			$U.redirectTo('user-set');
		}
	}

	const clear = () => {
		uni.removeStorageSync('targetEmail');
		store.commit('setTargetEmail', '');
		email.value = '';
	}
</script>

<style lang="less">

</style>
